const http = require("http");
const fs = require("fs");
const path = require("path");

var filePath = path.join(__dirname,"/index.html");

var reqUrl = 'http://nodejs.cn/';
//url.parse(reqUrl)可以将url的字符串转化为对象的结构
var obj = url.parse(reqUrl);

var options = {
    hostname:"nodejs.cn",
    port:80,
    path:"/",
    method:"GET",
}


var request = http.request(options,(res)=>{
    var str="";
    res.on("data",(chunk)=>{
        str += chunk;
        fs.writeFileSync(filePath,chunk,function(err){
            if(err){
                console.log(err);
            }
        })
    })
    res.on("end",()=>{
        console.log(str);
    })

})
//http.request方法一定要调用end方法
request.end();